package com.jy.importclient.support;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.importclient.common.dto.ReturnDto;
import com.jy.importclient.common.vo.ReturnVo;
import com.jy.importclient.handler.SubtaskHandler;
import com.jy.importclient.properties.DataPipelineApiProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @类名 HttpUtil
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/5/10 00:29
 * @版本 1.0
 */
@Component
public class HttpSupport {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SubtaskHandler subtaskHandler;

    @Resource
    private DataPipelineApiProperties properties;

    public void send(ReturnVo returnVo) {
        try {
            String message = restTemplate.postForObject(properties.getUrl(), JSONObject.toJSONString(returnVo), String.class);
            ReturnDto returnDto = JSONObject.parseObject(JSONObject.toJSONString(JSON.parse(message)), ReturnDto.class);
            if (ObjectUtil.isNotNull(returnDto)) {
                subtaskHandler.handle(returnDto);
            }
        } catch (Exception ignore) {}
    }
}